package com.hangwei.aicabinet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hangwei.aicabinet.R;

import java.util.ArrayList;

/**
 * Created by 张龙臣 on 2018/11/18.
 */

public class DoorColorAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private ArrayList<Integer> mList;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_color, null);
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TextView tvColor = holder.itemView.findViewById(R.id.tvColor);
        View leftView = holder.itemView.findViewById(R.id.viewLeft);
        View rightView = holder.itemView.findViewById(R.id.viewRight);
        if (position == 0 || position == 2 || position == 4 || position == 6) {
            leftView.setVisibility(View.GONE);
            rightView.setVisibility(View.VISIBLE);
        } else {
            leftView.setVisibility(View.VISIBLE);
            rightView.setVisibility(View.GONE);
        }
        if (mList == null || mList.size() == 0) {
            tvColor.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
            return;
        }

        if (mList.contains(position))
            tvColor.setBackgroundColor(mContext.getResources().getColor(R.color.haveTool));
        else
            tvColor.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));

//        if (position == 3 || position == 5 || position == 6)
//            tvColor.setBackgroundColor(mContext.getResources().getColor(R.color.haveTool));
//        else
//            tvColor.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
    }

    @Override
    public int getItemCount() {
        return 8;
    }

    public void notifyDataSetChanged(Integer[] data) {
        mList = new ArrayList<>();
        if (data != null && data.length != 0) {
            for (Integer which:data) {
                switch (which){
                    case 1:
                        mList.add(0);
                        break;
                    case 2:
                        mList.add(2);
                        break;
                    case 3:
                        mList.add(4);
                        break;
                    case 4:
                        mList.add(6);
                        break;
                    case 5:
                        mList.add(1);
                        break;
                    case 6:
                        mList.add(3);
                        break;
                    case 7:
                        mList.add(5);
                        break;
                    case 8:
                        mList.add(7);
                        break;
                }
            }
        }
        notifyDataSetChanged();
    }

//    class MyHolder extends RecyclerView.ViewHolder
}
